/**
 * Created by jack on 18/3/25.
 */
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class BezierCutting extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private int pointnum = 0;
    private boolean flag = true,end = false;
    ArrayList<Point> point = new ArrayList<Point>();
    ArrayList<Point> tempPoint = new ArrayList<Point>();
    ArrayList<Point> tempPoint2 = new ArrayList<Point>();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        System.out.println("a problem");
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    BezierCutting frame = new BezierCutting();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public BezierCutting() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 400, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        this.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                Graphics graphics=getGraphics();
                if(e.getButton() == MouseEvent.BUTTON1) {  //左键画折线
                    if(flag) {
                        point.add(new Point(e.getX(),e.getY()));
                        pointnum++;
                        System.out.println("点"+(pointnum)+":("+e.getX()+","+e.getY()+")");
                        flag = false;
                    }else{
                        point.add(new Point(e.getX(),e.getY()));
                        pointnum++;
                        System.out.println("点"+(pointnum)+":("+e.getX()+","+e.getY()+")");

                        graphics.drawLine((int)point.get(pointnum-2).x,(int)point.get(pointnum-2).y,
                                e.getX(),e.getY());
                    }
                    if(end) {
                        repaint();
                        flag = true;
                        end = false;
                        pointnum = 0;
                        point.clear();
                    }
                }
                if(e.getButton() == MouseEvent.BUTTON3) {      //右键绘制曲线,割角算法
                    double sx=point.get(0).getX(),sy=point.get(0).getY(),ex=0,ey=0,tx=0,ty=0;
                    int n=pointnum-1;
                    for(double t=0;t<=1;t+=0.1){ //控制求得曲线上点的数量
                        tempPoint.clear();
                        tempPoint.addAll(point); //把所有数据点赋值给tempPoint

                        for(int i=0;i<n;i++){
                            for(int j=0;j<tempPoint.size()-1;j++){
                                //计算分点坐标
                                tx=tempPoint.get(j).getX()*(1-t)+tempPoint.get(j+1).getX()*t;
                                ty=tempPoint.get(j).getY()*(1-t)+tempPoint.get(j+1).getY()*t;
                                tempPoint2.add(new Point((int)tx, (int)ty));
                            }
                            tempPoint.clear();
                            tempPoint.addAll(tempPoint2);
                            tempPoint2.clear(); //清空保存求得的分点
                        }

                        ex=tempPoint.get(0).getX();
                        ey=tempPoint.get(0).getY();
                        graphics.setColor(Color.RED);
                        graphics.drawLine((int)sx, (int)sy, (int)ex, (int)ey);
                        sx=ex;sy=ey; //保存本次的点，作为下次画线的起始点
                    }

                    System.out.println("PointNum"+point.size());
                    System.out.println("绘制完成");
                    end=true;
                }
            }
        });
    }
}

